package com.taxi.driver.module.account.code;

import android.os.Handler;
import android.util.Log;

import com.qianxx.network.RequestError;
import com.qianxx.utils.RxUtil;
import com.qianxx.utils.encode.RandomUtil;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.data.user.UserRepository;

import javax.inject.Inject;

/**
 * 功能描述：
 */
public class CodePresenter extends BasePresenter implements CodeContract.Presenter {

    private final static int COOLING_TIME_COUNT = 60;

    CodeContract.View mView;
    UserRepository mUserRepository;
    private int mRemainTime; //单位秒
    private Handler mHandler = new Handler();
    private Runnable mRunnable = new Runnable() {
        @Override
        public void run() {
            mHandler.removeCallbacks(mRunnable);
            mRemainTime--;
            mView.setTimerDisplay(mRemainTime);
            /* 继续定时 */
            if (mRemainTime > 0)
                mHandler.postDelayed(mRunnable, 1000);
        }
    };

    @Inject
    public CodePresenter(CodeContract.View view, UserRepository userRepository) {
        mView = view;
        mUserRepository = userRepository;
    }

    public void onDestory() {
        if (mHandler != null) mHandler.removeCallbacks(mRunnable);
    }

    @Override
    public void sendCode(String phone) {
        mSubscriptions.add(mUserRepository.sendCode(RandomUtil.RandomEncrypt(phone))
                .compose(RxUtil.applySchedulers())
                .doOnSubscribe(() -> mView.showLoadingView(true))
                .doOnCompleted(() -> mView.hideLoadingView())
                .subscribe(s -> {
                    mView.sendCodeSuccess();
                    /* 开启定时 */
                    mHandler.removeCallbacks(mRunnable);
                    mRemainTime = COOLING_TIME_COUNT;
                    mView.setTimerDisplay(mRemainTime);
                    mHandler.postDelayed(mRunnable, 1000);
                }, ex -> {
                    showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                }));
    }

    @Override
    public void verifyCode(String phone, final String code) {
        Log.i("ss--",RandomUtil.RandomEncrypt(phone)+","+RandomUtil.RandomEncrypt(code));
        mUserRepository.verifyCode(RandomUtil.RandomEncrypt(phone), RandomUtil.RandomEncrypt(code))
                .compose(RxUtil.applySchedulers())
                .doOnSubscribe(() -> mView.showLoadingView(true))
                .doOnCompleted(() -> mView.hideLoadingView())
                .subscribe(s -> {
                    mView.verifyCodeSuccess(code);
                }, ex -> {
                    showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                    if (ex instanceof RequestError) {
                        RequestError error = ((RequestError) ex);
                        if (error.getMessage() != null
                                && error.getMessage().contains("验证码")) {
                            mView.resetCodeInput();
                        }
                    }
                });
    }

}
